package com.typing.controller;

import com.typing.entity.AdminEntity;
import com.typing.entity.FileEntity;
import com.typing.entity.UserEntity;
import com.typing.service.AdminService;
import com.typing.service.UserService;
import com.typing.utils.FileTools;
import com.typing.utils.Result;
import com.typing.utils.Tools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

	@Resource
	UserService userService;

	@Resource
	AdminService adminService;

	private static String USER = "user";

	/**
	 * 用于用户登录操作
	 *
	 * @param user
	 * @return
	 */
	@PostMapping("/userLogin")
	public Result userLogin(@RequestBody UserEntity user) {

		//	对手机号码格式进行判断
		if (!Tools.isPhone(user.getMobilePhone())) {
			//	手机号码不合法
			return new Result().mobilePhoneError();
		}
		//	判断该手机号有没有注册
		UserEntity userEntities = userService.isRegister(user);
		if (userEntities == null) {
			return new Result().notRegisteredError();
		}
		//	判断密码格式合不合法
		if (!Tools.isPassword(user.getPassword())) {
			return new Result().pwFormatError();
		}
		//	密码格式正确，进行登录
		UserEntity userList = userService.userLogin(user);
		if (userList == null) {
			//	密码错误
			return new Result().passwordError();
		}

		user.setTag(UUID.randomUUID().toString());
		userService.setTag(user);
		UserEntity newTagUser = userService.getNewUser(user);
		//	返回带着新的tag值的用户对象信息
		return new Result().ok(newTagUser);
	}

	/**
	 * 注册用户
	 *
	 * @param user
	 * @return
	 */

	@Value("${config.h_savePath}")
	private String savePath;

	@Value("${config.h_relativePath}")
	private String relativePath;

	@PostMapping("/addUser")
	public Result addUser(@RequestBody UserEntity user
	 // TODO: 2022/11/22 要使用头像功能删除@RequestBody，注释解开即可
//	, @RequestParam("file") MultipartFile file
	) {

		//	手机号格式判断
		if (!Tools.isPhone(user.getMobilePhone())) {
			return new Result().mobilePhoneError();
		}
		//	用户名非空判断
		if (!Tools.isRightParam(user.getUsername())) {
			return new Result().userNameError();
		}
		//	密码格式判断
		if (!Tools.isPassword(user.getPassword())) {
			return new Result().pwFormatError();
		}
		//	学院非空判断
		if (!Tools.isRightParam(user.getCollege())) {
			return new Result().collegeError();
		}
		//	专业非空判断
		if (!Tools.isRightParam(user.getMajor())) {
			return new Result().majorError();
		}
		//	班级非空判断
		if (!Tools.isRightParam(user.getClasses())) {
			return new Result().classesError();
		}
		//	身份非空判断
		if (!Tools.isRightParam(user.getRole())) {
			return new Result().roleFormatError();
		}
		//	如果文件不为空执行上传
//		if (FileTools.hasFile(file.getOriginalFilename())){
//			FileEntity fileEntity = new FileEntity();
//
//			fileEntity.setSavePath(savePath);
//			fileEntity.setRelativePath(relativePath);
//
//			FileEntity f_Entity = FileTools.uploadFile(file, fileEntity);
//			user.setHeadPortrait(f_Entity.getRelativePath()+f_Entity.getFileName());
//		}
		//	该用户是否注册
		UserEntity register = userService.isRegister(user);
		if (register != null) {
			return new Result().phoneIsRegistered();
		}
		//	进行注册
		int i = userService.addUser(user);
		if (i != 1) {
			return new Result().systemError();
		}
		return new Result().ok(user.getHeadPortrait(), "用户添加成功");
	}

	/**
	 * 管理员登录
	 *
	 * @param admin
	 * @return
	 */
	@PostMapping("/adminLogin")
	public Result adminLogin(@RequestBody AdminEntity admin) {
		//	对账号格式进行判断
		if (!Tools.isRightParam(admin.getUsername())) {
			return new Result().adminFormatError();
		}
		//	对密码格式进行判断
		if (!Tools.isPassword(admin.getPassword())) {
			return new Result().pwFormatError();
		}
		//	未注册
		AdminEntity adminEntity = adminService.isRegister(admin);
		if (adminEntity == null) {
			return new Result().adminNotRegistered();
		}
		//	进行登录
		AdminEntity adminEntities = adminService.adminLogin(admin);
		if (adminEntities == null) {
			return new Result().passwordError();
		}
		//	登录成功，设置登录标识
		admin.setTag(UUID.randomUUID().toString());
		adminService.setTag(admin);
		AdminEntity admins = adminService.getNewAdmin(admin);
		return new Result().ok(admins);
	}
}
